﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.Models;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class GaraManagerController : Controller
    {
        //
        // GET: /GaraManager/
        VNInsuranceDataContext db = new VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult listgara()
        {
            var benlang = db.GaraLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] ).Select(m => new
            {
                GaraID = m.GaraID,
                LangID = m.LangID,
                GaraName=m.GaraName,
                Adress = m.Address,
                Phone = m.Phone,
                Fax = m.Fax
            });
            return Json(benlang, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult AddGara()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddGara(GaraLang garalang)
        {
            Gara gr = new Gara();
            
            GaraLang gl = new GaraLang();
            GaraLang gl1 = new GaraLang();
            var isexit = db.Garas.Where(m => m.Garaname == garalang.GaraName).FirstOrDefault();
            if (isexit == null)
            {
                gr.Garaname = garalang.GaraName;
                db.Garas.InsertOnSubmit(gr);
                db.SubmitChanges();
                gl.GaraID = gr.ID;
                gl.GaraName = gr.Garaname;
                gl.LangID = Request.Cookies["_culture"]["lang"];
                gl.Address = garalang.Address;
                gl.Phone = garalang.Phone;
                gl.Fax = garalang.Fax;
                db.GaraLangs.InsertOnSubmit(gl);
                if (gl.LangID == "en-US")
                {
                    gl1.LangID = "vi-VN";
                }
                else
                {
                    gl1.LangID = "en-US";
                }
                gl1.GaraID = gr.ID;
                gl1.GaraName = gr.Garaname;
                gl1.Address = garalang.Address;
                db.GaraLangs.InsertOnSubmit(gl1);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return Json("Garaname is exist");
            }
        }
        [HttpGet]
        public ActionResult EditGara(int id)
        {
            var garaf = db.GaraLangs.Where(m => m.GaraID == id && m.LangID == Request.Cookies["_culture"]["lang"]).SingleOrDefault();
            return View(garaf);
        }
        [HttpPost]
        public ActionResult EditGara(GaraLang garalang)
        {
            var garafound = db.GaraLangs.Where(m => m.GaraID == garalang.GaraID && m.LangID == garalang.LangID).SingleOrDefault();
            if (garafound != null)
            {
                garafound.GaraName = garalang.GaraName;
                garafound.Address = garalang.Address;
                garafound.Phone = garalang.Phone;
                garafound.Fax = garalang.Fax;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
            
        }
        [HttpPost]
        public ActionResult RemoveGara(int id)
        {
            var garafound = db.GaraLangs.Where(m => m.GaraID ==id);
            db.GaraLangs.DeleteAllOnSubmit(garafound);
            db.SubmitChanges();
            var gara = db.Garas.Where(m => m.ID == id).SingleOrDefault();
            db.Garas.DeleteOnSubmit(gara);
            db.SubmitChanges();
            return Json(true);
        }

    }
}
